package org.lg.dao.impl;

import org.lg.config.Repository;
import org.lg.config.Resource;
import org.lg.dao.AccoutDao;
import org.lg.dao.StudentDao;
import org.lg.entity.Student;
import org.lg.entity.StudentExample;
import org.lg.mapper.StudentMapper;
import org.lg.page.PageInfo;
import org.lg.vo.Account;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Repository("studentDao")
public class StudentDaoImpl implements StudentDao, AccoutDao {
    @Resource
    StudentMapper studentMapper;
    @Override
    public List<Student> getAllStudents() throws Exception {
//        List<Student> students = Arrays.asList(
//        new Student[]{new Student("0089","男"),new Student("0049","女"),new Student("0087","男")});

        return studentMapper.selectByExample(null);
    }

    /**
     * 分页查询学生
     * @param pageInfo
     * @return
     */

    public List<Student> getStudentByPage(PageInfo pageInfo) throws Exception {
        StudentExample studentExample = new StudentExample();
        long l = studentMapper.countByExample(null);
        pageInfo.setItemCount((int)l);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        System.out.println("lllllllllllllllllll");
        return studentMapper.getStudentByPage(map);
    }

    /**
     * 按班级分页查询同学
     * @param pageInfo
     * @return
     */

    public List<Student> getClassMateByCnoAndByPage(PageInfo pageInfo) throws Exception {
        Map<String, String> conditions = pageInfo.getConditions();
        String cno = conditions.get("cno");
        String sno = conditions.get("sno");
        StudentExample studentExample = new StudentExample();
        studentExample.createCriteria().andCnoEqualTo(cno).andSnoNotEqualTo(sno);
        long l = studentMapper.countByExample(studentExample);
        pageInfo.setItemCount((int)l);
        studentExample.setLimit(pageInfo.getPageSize());
        studentExample.setOffset(pageInfo.getOffset());
        return studentMapper.selectByExample(studentExample);
    }


    /**
     * 根据主键查询的学生关联班级和班主任信息
     * @param sno
     * @return
     * @throws Exception
     */
    @Override
    public Student selectByPrimaryKeyWithClazzAndTeacher(String sno)  throws Exception{
        return studentMapper.selectByPrimaryKeyWithClazzAndTeacher(sno);
    }

    @Override
    public Student getStudentBySno(String sno) throws Exception {
        return studentMapper.selectByPrimaryKey(sno);
    }

    @Override
    public List<Student> getStudentByCno(String cno) throws Exception {
        return studentMapper.selectStudentByCno(cno);
    }

    /**
     * 查询成绩前20的同学
     * @return
     * @throws Exception
     */
    public List<Student> getTop20Student() throws Exception {

        StudentExample studentExample = new StudentExample();
        studentExample.setOrderByClause("avgscore desc");
        studentExample.setLimit(20);
        return studentMapper.selectByExample(studentExample);
    }


    @Override
    public Integer addStudent(Student student) throws Exception {
        return studentMapper.insertSelective(student);
    }

    @Override
    public Integer editStudent(Student student) throws Exception {
        return studentMapper.updateByPrimaryKeySelective(student);
    }


    @Override
    public Account getAccountByUsername(String username) throws Exception {
        StudentExample studentExample = new StudentExample();
        studentExample.createCriteria().andNameEqualTo(username);
        List<Student> students = studentMapper.selectByExample(studentExample);
        Optional<List<Student>> op = Optional.ofNullable(students);
        return op.isPresent()&&op.get().size()>0?op.get().get(0):null;
    }
}
